<?php

namespace App\Repositories;

use App\Exceptions\BasicException;

class SexAndLoveRepository
{
    /**
     * @Desc:列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:17
     */
    public function list($param)
    {
        $list = app('model_user_sex_and_love')
            ->where(function ($query) use ($param) {
            if (isset($param['type'])) {
                $query->where('type', $param['type']);
            }
            if (isset($param['status'])) {
                $query->where('status', $param['status']);
            }
            if (isset($param['title']) && $param['title']) {
                $query->where('title', 'like','%'.$param['title'].'%');
            }
        })->orderBy('id', 'desc')
            ->paginate($param['page_size'] ?? 10, ['*'], '', $param['page'] ?? 1);
        return $list;
    }


    /**
     * @Desc:新增
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function create($params)
    {
        $data = [
            'title' => $params['title'],
            'type' => $params['type'],
            'image' => $params['image'],
            'explain' => $params['explain'] ?? '',
            'status' => 1, //默认开启
        ];

        $res = app('model_user_sex_and_love')->create($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @Desc:详情
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function info($param)
    {
        $info = app('model_user_sex_and_love')
            ->where('id', $param['id'])
            ->first();
        return $info;
    }

    /**
     * @Desc:更新
     * @param $params
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function update($params)
    {
        $id = $params['id'];
        $data = [
            'title' => $params['title'],
            'image' => $params['image'],
            'explain' => $params['explain'] ?? '',
        ];

        $res = app('model_user_sex_and_love')->where('id', $id)->update($data);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    /**
     * @Desc:删除
     * @param $param
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/1 15:16
     */
    public function delete($param)
    {
        $res = app('model_user_sex_and_love')->where('id', $param['id'])->delete();
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '删除失败');
        }
    }

    /**
     * @Desc:更新状态
     * @param $param
     * @return bool
     * @author: shiquan
     * @Time: 2022/11/8 15:13
     */
    public function updateStatus($param)
    {
        app('model_user_sex_and_love')->where('id', $param['id'])->update([
            'status' => $param['status']
        ]);
        return true;
    }
}
